﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.Entities;

namespace LibrarySystem.LibraryUtils.DAL.DAOs
{
    public class Library_Books : Common
    {


        public void UpdateQuantity(int id, int quantity)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Book book = _context.Library_Books.Where(b => b.Id == id).First();
            book.Quantity = book.Quantity + quantity;
            book.BorrowTimes = ++book.BorrowTimes;
            _context.SubmitChanges();
            //
            _context.Dispose();
        }

        public List<BookDetailModel> GetBookDetail(string bookCode = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Book> queryBook = _context.Library_Books;
            //Filter
            if(string.IsNullOrEmpty(bookCode))
            {
                return null;             
            }
            queryBook = queryBook.Where(b => b.Code == bookCode);
            List<Library_Book> booksSort = queryBook.ToList();

            List<BookDetailModel> bookList = new List<BookDetailModel>();
            foreach (Library_Book lb in booksSort)
            {
                BookDetailModel model = new BookDetailModel();
                model.ID = lb.Id;
                model.BookCode = lb.Code;
                model.BookName = lb.Name;
                model.BookAuthor = lb.Author;
                model.BookCategory = lb.Library_Category.Name;
                model.Description = lb.Description;
                model.IsContainDisk = (Boolean)lb.IsContainDisk;
                model.Photo = lb.Photo;
                bookList.Add(model);
            }
            _context.Dispose();
            return bookList;
        }

        public int GetDataCount(string bookCode = null, string bookName = null, string bookAuthor = null, int category = 0)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Book> queryBook = _context.Library_Books;
            //Filter
            if (!string.IsNullOrEmpty(bookCode))
            {
                queryBook = queryBook.Where(b => b.Code == bookCode);
            }
            if (!string.IsNullOrEmpty(bookName))
            {
                queryBook = queryBook.Where(b => b.Name.Contains(bookName));
            }
            if (!string.IsNullOrEmpty(bookAuthor))
            {
                queryBook = queryBook.Where(b => b.Author.Contains(bookAuthor));
            }
            if (category > 0)
            {
                queryBook = queryBook.Where(b => b.Library_Category.Id == category);
            }
            return queryBook.Count();
        }

        public List<BookModel> GetDataList(string bookCode = null, string bookName = null, string bookAuthor = null, int category = 0, int startIndex = 0, int pageSize = 0, string sorting = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Book> queryBook = _context.Library_Books;
            //Filter
            if (!string.IsNullOrEmpty(bookCode))
            {
                queryBook = queryBook.Where(b => b.Code == bookCode);
            }
            if (!string.IsNullOrEmpty(bookName))
            {
                queryBook = queryBook.Where(b => b.Name.Contains(bookName));
            }
            if (!string.IsNullOrEmpty(bookAuthor))
            {
                queryBook = queryBook.Where(b => b.Author.Contains(bookAuthor));
            }
            if (category > 0)
            {
                queryBook = queryBook.Where(b => b.Library_Category.Id == category);
            }
            //Sorting
            #region Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("Name ASC"))
            {
                queryBook = queryBook.OrderBy(b => b.Name);
            }
            else if (sorting.Equals("Name DESC"))
            {
                queryBook = queryBook.OrderByDescending(b => b.Name);
            }
            else if (sorting.Equals("BorrowTimes ASC"))
            {
                queryBook = queryBook.OrderBy(b => b.BorrowTimes);
            }
            else if (sorting.Equals("BorrowTimes DESC"))
            {
                queryBook = queryBook.OrderByDescending(b => b.BorrowTimes);
            }
            else if (sorting.Equals("BookCode ASC"))
            {
                queryBook = queryBook.OrderBy(b => b.Code);
            }
            else if (sorting.Equals("BookCode DESC"))
            {
                queryBook = queryBook.OrderByDescending(b => b.Code);
            }
            else if (sorting.Equals("BookName ASC"))
            {
                queryBook = queryBook.OrderBy(b => b.Name);
            }
            else if (sorting.Equals("BookName DESC"))
            {
                queryBook = queryBook.OrderByDescending(b => b.Name);
            }
            else if (sorting.Equals("BookAuthor ASC"))
            {
                queryBook = queryBook.OrderBy(b => b.Author);
            }
            else if (sorting.Equals("BookAuthor DESC"))
            {
                queryBook = queryBook.OrderByDescending(b => b.Author);
            }
            else if (sorting.Equals("BookCategory ASC"))
            {
                queryBook = queryBook.OrderBy(b => b.CategoryID);
            }
            else if (sorting.Equals("BookCategory DESC"))
            {
                queryBook = queryBook.OrderByDescending(b => b.CategoryID);
            }
            else if (sorting.Equals("BookQuantity ASC"))
            {
                queryBook = queryBook.OrderBy(b => b.Quantity);
            }
            else if (sorting.Equals("BookQuantity DESC"))
            {
                queryBook = queryBook.OrderByDescending(b => b.Quantity);
            }
            else if (sorting.Equals("BorrowTimes ASC"))
            {
                queryBook = queryBook.OrderBy(b => b.BorrowTimes);
            }
            else if (sorting.Equals("BorrowTimes DESC"))
            {
                queryBook = queryBook.OrderByDescending(b => b.BorrowTimes);
            }
            #endregion
            //
            List<Library_Book> booksSort = pageSize > 0 ?
                        queryBook.Skip(startIndex).Take(pageSize).ToList()
                        : queryBook.ToList();

            List<BookModel> bookList = new List<BookModel>();

            foreach (Library_Book lb in booksSort)
            {
                BookModel model = new BookModel();
                model.ID = lb.Id;
                model.BookCode = lb.Code;
                model.BookName = lb.Name;
                model.BookAuthor = lb.Author;
                model.BookCategory = lb.CategoryID;
                model.BookQuantity = lb.Quantity;
                model.PricePerUnit = lb.PricePerUnit;
                model.Storage = lb.Storage;
                model.Description = lb.Description;
                model.IsContainDisk = (Boolean)lb.IsContainDisk;
                model.Photo = lb.Photo;
                model.FirstInputedDate = (DateTime)lb.FirstInputedDate;
                model.BorrowTimes = lb.BorrowTimes;
                bookList.Add(model);
            }
            _context.Dispose();
            return bookList;
        }

        public BookModel AddNewData(BookModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Book lib = new Library_Book();
            lib.Code = data.BookCode;
            lib.Name = data.BookName;
            lib.Author = data.BookAuthor;
            lib.CategoryID = data.BookCategory;
            lib.Quantity = data.BookQuantity;
            lib.PricePerUnit = data.PricePerUnit;
            lib.Storage = data.Storage;
            lib.Description = data.Description;
            lib.IsContainDisk = (bool)data.IsContainDisk ? true : false;
            lib.Photo = data.Photo;
            lib.FirstInputedDate = (DateTime)data.FirstInputedDate;
            lib.BorrowTimes = data.BorrowTimes;
            //
            _context.Library_Books.InsertOnSubmit(lib);
            _context.SubmitChanges();
            //Gan gia tri moi insert lai cho Model
            data.ID = lib.Id;
            _context.Dispose();
            return data;
        }

        public bool UpdateData(BookModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Book lib = _context.Library_Books.Where(b => b.Id == data.ID).FirstOrDefault<Library_Book>();
            //
            lib.Code = data.BookCode;
            lib.Name = data.BookName;
            lib.Author = data.BookAuthor;
            lib.CategoryID = data.BookCategory;
            lib.Quantity = data.BookQuantity;
            lib.PricePerUnit = data.PricePerUnit;
            lib.Storage = data.Storage;
            lib.Description = data.Description;
            lib.IsContainDisk = (bool)data.IsContainDisk ? true : false;
            lib.Photo = data.Photo;
            lib.FirstInputedDate = (DateTime)data.FirstInputedDate;
            lib.BorrowTimes = data.BorrowTimes;
            _context.SubmitChanges();
            _context.Dispose();
            return true;
        }

        public bool DeleteData(int Id)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code

            //
            _context.Dispose();
            return true;
        }
    }
}